import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login'
import webStorage from '../config/webStorage'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
//vue登录逻辑
router.beforeEach(function (to, form, next) {
  console.log("to:",to)
  console.log("form:",form)
  if (to["path"] === "/login") {   //只有需要登录时候才跳转到登陆页面
    if (webStorage.sessionGet(webStorage.GetSessionAuthStateKeyName())) {
      return next({path: "/"});
    }
    return next()
  }
  if (!webStorage.sessionGet(webStorage.GetSessionAuthStateKeyName())) {
    return next({path: "/login"});
  }
  next();
});
export default router
